// 1. 转换js、scss、html、image、font
// 2. 创建服务器、监听文件热更新
// 3. 上线准备，文件清空、文件压缩
const data = require('./data');
const browserSync = require('browser-sync')

const bs = browserSync.create()
module.exports = grunt => {
  require('load-grunt-tasks')(grunt);

  const clean = {
    temp: 'temp',
    dist: 'dist',
    tempHtml: 'temp/src'
  }

  const sass = {
    temp: {
      files: [{
        expand: true,
        cwd: 'src/assets/styles',
        src: ['*.scss'],
        dest: 'temp/assets/styles',
        ext: '.css'
      }]
    }
  }

  const babel = {
    options: {
      sourceMap: true,
      presets: ['@babel/preset-env']
    },
    temp: {
      files: [{
        expand: true,
        cwd: 'src/assets/scripts',
        src: ['*.js'],
        dest: 'temp/assets/scripts',
      }]
    }
  }

  const swigtemplates = {
    build: {
      context: data,
      src: ['src/*.html'],
      dest: 'temp',
    }
  }

  const imagemin = {
    dynamic: {
      files: [
        {
          expand: true,
          cwd: 'src/assets/images',
          src: ['*'],
          dest: 'dist/assets/images'
        },
        {
          expand: true,
          cwd: 'src/assets/fonts',
          src: ['*'],
          dest: 'dist/assets/fonts'
        }
      ]
    }
  }

  const watch = {
    scripts: {
      files: ['src/assets/scripts/*.js'],
      tasks: ['babel'],
    },
    styles: {
      files: ['src/assets/scripts/*.scss'],
      tasks: ['sass'],
    },
    htmls: {
      files: ['src/*.html'],
      tasks: ['swig'],
    }
  }

  const uglify = {
    default: {
      files: [{
        expand: true,
        cwd: 'temp/assets/scripts',
        src: '*.js',
        dest: 'dist/assets/scripts'
      }]
    }
  }

  const cssmin = {
    default: {
      files: [{
        expand: true,
        cwd: 'temp/assets/styles',
        src: ['*.css', '!*.min.css'],
        dest: 'dist/assets/styles',
        ext: '.min.css'
      }]
    }
  }

  const htmlmin = {
    options: {
      removeComments: true,
      collapseWhitespace: true
    },
    default: {
      files: [{
        expand: true,
        cwd: 'temp/src',
        src: ['*.html'],
        dest: 'dist'
      }]
    }
  }

  const copy = {
    public: {
      expand: true,
      cwd: 'public',
      src: '*',
      dest: 'dist'
    },
    html: {
      expand: true,
      cwd: 'temp/src',
      src: '*.html',
      dest: 'temp'
    }
  }

  const useminPrepare = {
    html: ['temp/*.html'],
  }

  const usemin = {
    html: ['temp/*.html'],
  }

  const shell = {
    fileChmod: {
      command: 'chmod -Rf 777 .tmp'
    }
  }

  // grunt.initConfig({
  //   clean, sass, babel, swigtemplates,
  //   watch, copy, useminPrepare, usemin, 
  //   imagemin, uglify, htmlmin, shell
  // });

  grunt.initConfig({
    clean, sass, babel, swigtemplates,
    watch, copy, useminPrepare, usemin, 
    imagemin, uglify, htmlmin, shell
  });

  grunt.registerTask('web-server', () => {
    bs.init({
      notify: false,
      port: 8083,
      server: {
        baseDir: ['temp', 'temp/src', 'src', 'public'],
        routes: {
          '/node_modules': 'node_modules'
        }
      }
    })
  })

  grunt.registerTask('compile', ['sass', 'babel', 'swigtemplates']);

  grunt.registerTask('serve', ['compile', 'web-server', 'watch']);

  grunt.registerTask('compress', ['htmlmin', 'imagemin', 'copy:public']);

  grunt.registerTask('preBuild', [
    'clean:temp',
    'swigtemplates',
    'copy:html',
    'clean:tempHtml',
    'useminPrepare',
    'concat:generated',
    'shell:fileChmod',
    'cssmin:generated',
    'uglify:generated',
    'usemin',
  ]);

  grunt.registerTask('build', [
    'clean',
    'compile',
    'preBuild',
    'compress',
  ]);
  // 本地环境，只需要开启serve (内部自带compile)
  // 上线 clean -> compile -> compress
}